<?php

namespace Pader\DcatAdmin\ChinaDistpicker;

use Dcat\Admin\Form\Field;
use Illuminate\Support\Arr;

class Distpicker extends Field
{
    /**
     * @var string
     */
    protected $view = 'pader-china-distpicker::select';

    /**
     * @var array
     */
    protected static $js = [
        'vendor/dcat-admin-extensions/pader/dcat-admin-china-distpicker/dist/distpicker.min.js'
    ];

    /**
     * @var array
     */
    protected $placeholder = [];

    /**
     * Distpicker constructor.
     *
     * @param int $column
     * @param array $arguments
     */
    public function __construct($column, array $arguments=[])
    {



        if(isset($arguments[1])){
            $this->view = 'pader-china-distpicker::'.$arguments[1];
        }

        $this->column = $column;
        $this->placeholder = $arguments['placeholder'] ?? [];
        $this->label = $arguments[0]['label'] ?? '地区选择';
    }

    protected function prepareInputValue($value)
    {
        if (is_array($value)) {
            //2022年3月9日17点57分 优化多级选择
            $code = isset($value['district']) ?$value['district']: ( isset($value['city']) ?$value['city']: $value['province']);

            return $code;
        } else {
            return $value;
        }
    }

    public function getValidator(array $input)
    {
        $value = Arr::get($input, $this->column);
        if (empty($value)){
            return false;
        }
     //   $code = $value['district'] ?: $value['city'] ?: $value['province'];
        //2022年3月9日17点57分 优化多级选择

        $code = (isset($value['district']) && $value['district'] != 0) ?$value['district']: ( (isset($value['city'])&& $value['district'] != 0) ?$value['city']: $value['province']);
        Arr::set($input, $this->column, $code);

        if ($this->validator) {
            return $this->validator->call($this, $input);
        }

        $rules = $attributes = [];

        if (!$fieldRules = $this->getRules()) {
            return false;
        }

        $input[$this->column] = $code;
        $rules[$this->column] = $fieldRules;
        $attributes[$this->column] = $this->label . "[{$this->column}]";

        return \validator($input, $rules, $this->getValidationMessages(), $attributes);
    }

    /**
     * @param int $count
     * @return $this
     */
    public function autoselect($count = 0)
    {
        return $this->attribute('data-autoselect', $count);
    }

    /**
     * {@inheritdoc}
     */
    public function render()
    {
        $this->attribute('data-value-type', 'code');

        $value = old($this->column, $this->value());

        if ($value) {
            $province   = substr($value, 0, 2).'0000';
            $city       = substr($value, 0, 4).'00';
            if ($city == $province){
                $city       = null;
                $district   = null;
            }else{
                if ($city == $value){
                    $district   = null;
                }else{
                    $district   = $value;
                }
            }
        } else {
            $province   = $this->placeholder['province'] ?? null;
            $city       = $this->placeholder['city'] ?? null;
            $district   = $this->placeholder['district'] ?? null;
        }

        $id = uniqid('distpicker-');

        $this->script = <<<EOT
$("#{$id}").distpicker({
  province: '$province',
  city: '$city',
  district: '$district'
});
EOT;

        $this->addVariables(compact('id'));

        return parent::render();
    }
}
